package com.example.Bean;

import java.io.IOException;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.example.Table.Question;
import com.example.util.HibernateUtil;

@ManagedBean(name = "buttonBean", eager = true)
@SessionScoped
public class ButtonBean {

	@ManagedProperty(value = "#{questionBean}")
	private QuestionBean questionBean;
	@ManagedProperty(value = "#{helperBean}")
	private HelperBean helperBean;
	@ManagedProperty(value = "#{scoreBean}")
	private ScoreBean scoreBean;

	private boolean button_a;
	private boolean button_b;
	private boolean button_c;
	private boolean button_d;
	private boolean button_fifty;
	private boolean button_call;
	private boolean button_auditory;
	public boolean isButton_fifty() {
		return button_fifty;
	}

	public void setButton_fifty(boolean button_fifty) {
		this.button_fifty = button_fifty;
	}

	public boolean isButton_call() {
		return button_call;
	}

	public void setButton_call(boolean button_call) {
		this.button_call = button_call;
	}

	public boolean isButton_auditory() {
		return button_auditory;
	}

	public void setButton_auditory(boolean button_auditory) {
		this.button_auditory = button_auditory;
	}

	private boolean showAlert;

	public boolean isShowAlert() {
		return showAlert;
	}

	public void setShowAlert(boolean showAlert) {
		this.showAlert = showAlert;
	}

	public QuestionBean getQuestionBean() {
		return questionBean;
	}

	public HelperBean getHelperBean() {
		return helperBean;
	}

	public ScoreBean getScoreBean() {
		return scoreBean;
	}

	public void setQuestionBean(QuestionBean questionBean) {
		this.questionBean = questionBean;
	}

	public void setHelperBean(HelperBean helperBean) {
		this.helperBean = helperBean;
	}

	public void setScoreBean(ScoreBean scoreBean) {
		this.scoreBean = scoreBean;
	}

	public boolean isButton_a() {
		return button_a;
	}

	public boolean isButton_b() {
		return button_b;
	}

	public boolean isButton_c() {
		return button_c;
	}

	public boolean isButton_d() {
		return button_d;
	}

	public String forward() {
		questionBean.forward();
		helperBean.forward();
		scoreBean.forward();
		button_a = false;
		button_b = false;
		button_c = false;
		button_d = false;
		button_auditory=false;
		button_call=false;
		button_fifty=false;
		try {
			FacesContext ctx = FacesContext.getCurrentInstance();
			ExternalContext ectx = ctx.getExternalContext();
			HttpServletRequest request = (HttpServletRequest) ectx.getRequest();
			HttpServletResponse response = (HttpServletResponse) ectx
					.getResponse();
			response.sendRedirect(request.getContextPath()
					+ "/faces/new_game.xhtml");
		} catch (IOException ex) {
		}
		return "Gus";
	}

	public String fifty_fifty() {
		button_fifty=true;
		String answer = questionBean.getAnswer();
		if (answer.equals(questionBean.getVariant_a())) {
			button_c = true;
			button_d = true;
		} else if (answer.equals(questionBean.getVariant_b())) {
			button_a = true;
			button_d = true;
		} else if (answer.equals(questionBean.getVariant_c())) {
			button_b = true;
			button_d = true;
		} else if (answer.equals(questionBean.getVariant_d())) {
			button_a = true;
			button_b = true;
		}
		try {
			FacesContext ctx = FacesContext.getCurrentInstance();
			ExternalContext ectx = ctx.getExternalContext();
			HttpServletRequest request = (HttpServletRequest) ectx.getRequest();
			HttpServletResponse response = (HttpServletResponse) ectx
					.getResponse();
			response.sendRedirect(request.getContextPath()
					+ "/faces/new_game.xhtml");
		} catch (IOException ex) {
		}
		return "fifty_fifty";
	}

	public void auditoryHelp() {
		button_auditory=true;
		showAlert = true;
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				showAlert = false;
			}
		});
		t.start();
	}

	private boolean callFriend;

	public boolean isCallFriend() {
		return callFriend;
	}

	public void setCallFriend(boolean callFriend) {
		this.callFriend = callFriend;
	}

	public void callTheFriend() {
		button_call=true;
		callFriend = true;
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				callFriend = false;
			}
		});
		t.start();
	}
	
	public void check_A(){
		if(questionBean.getVariant_a().equals(questionBean.getAnswer())){
			questionViewed();

			if(scoreBean.getCurrent_money()==0) scoreBean.setCurrent_money(500);
			else if(scoreBean.getCurrent_money()==500) scoreBean.setCurrent_money(1000);
			else if(scoreBean.getCurrent_money()==1000) scoreBean.setCurrent_money(2000);
			else if(scoreBean.getCurrent_money()==2000) scoreBean.setCurrent_money(4000);
			else if(scoreBean.getCurrent_money()==5000) scoreBean.setCurrent_money(10000);
			else if(scoreBean.getCurrent_money()==10000) scoreBean.setCurrent_money(15000);
			else if(scoreBean.getCurrent_money()==15000) scoreBean.setCurrent_money(25000);
			else if(scoreBean.getCurrent_money()==25000) scoreBean.setCurrent_money(50000);
			else if(scoreBean.getCurrent_money()==50000) scoreBean.setCurrent_money(100000);
			else if(scoreBean.getCurrent_money()==100000) scoreBean.setCurrent_money(150000);
			else if(scoreBean.getCurrent_money()==150000) scoreBean.setCurrent_money(250000);
			else if(scoreBean.getCurrent_money()==250000) scoreBean.setCurrent_money(500000);
			else if(scoreBean.getCurrent_money()==500000) scoreBean.setCurrent_money(1000000);
			else if(scoreBean.getCurrent_money()==1000000) scoreBean.setCurrent_money(2000000);
			else if(scoreBean.getCurrent_money()==2500000) scoreBean.setCurrent_money(5000000);
			else if(scoreBean.getCurrent_money()==5000000) redirectExit();
			forward();
		}
		else {
			if(scoreBean.getCurrent_money()<=10000) scoreBean.setCurrent_money(0);
			else if(scoreBean.getCurrent_money()<150000 && scoreBean.getCurrent_money()>=10000) scoreBean.setCheck_point(10000);
			else if(scoreBean.getCurrent_money()>=150000) scoreBean.setCheck_point(150000);
			redirectExit();
		}
	}
	public void check_B(){
		if(questionBean.getVariant_b().equals(questionBean.getAnswer())){
			questionViewed();
			if(scoreBean.getCurrent_money()==0) scoreBean.setCurrent_money(500);
			else if(scoreBean.getCurrent_money()==500) scoreBean.setCurrent_money(1000);
			else if(scoreBean.getCurrent_money()==1000) scoreBean.setCurrent_money(2000);
			else if(scoreBean.getCurrent_money()==2000) scoreBean.setCurrent_money(4000);
			else if(scoreBean.getCurrent_money()==5000) scoreBean.setCurrent_money(10000);
			else if(scoreBean.getCurrent_money()==10000) scoreBean.setCurrent_money(15000);
			else if(scoreBean.getCurrent_money()==15000) scoreBean.setCurrent_money(25000);
			else if(scoreBean.getCurrent_money()==25000) scoreBean.setCurrent_money(50000);
			else if(scoreBean.getCurrent_money()==50000) scoreBean.setCurrent_money(100000);
			else if(scoreBean.getCurrent_money()==100000) scoreBean.setCurrent_money(150000);
			else if(scoreBean.getCurrent_money()==150000) scoreBean.setCurrent_money(250000);
			else if(scoreBean.getCurrent_money()==250000) scoreBean.setCurrent_money(500000);
			else if(scoreBean.getCurrent_money()==500000) scoreBean.setCurrent_money(1000000);
			else if(scoreBean.getCurrent_money()==1000000) scoreBean.setCurrent_money(2000000);
			else if(scoreBean.getCurrent_money()==2500000) scoreBean.setCurrent_money(5000000);
			else if(scoreBean.getCurrent_money()==5000000) redirectExit();
			forward();
		}
		else {
			if(scoreBean.getCurrent_money()<=10000) scoreBean.setCurrent_money(0);
			else if(scoreBean.getCurrent_money()<150000 && scoreBean.getCurrent_money()>=10000) scoreBean.setCheck_point(10000);
			else if(scoreBean.getCurrent_money()>=150000) scoreBean.setCheck_point(150000);
			redirectExit();
		}
	}
	public void check_C(){
		if(questionBean.getVariant_c().equals(questionBean.getAnswer())){
			questionViewed();
			if(scoreBean.getCurrent_money()==0) scoreBean.setCurrent_money(500);
			else if(scoreBean.getCurrent_money()==500) scoreBean.setCurrent_money(1000);
			else if(scoreBean.getCurrent_money()==1000) scoreBean.setCurrent_money(2000);
			else if(scoreBean.getCurrent_money()==2000) scoreBean.setCurrent_money(4000);
			else if(scoreBean.getCurrent_money()==5000) scoreBean.setCurrent_money(10000);
			else if(scoreBean.getCurrent_money()==10000) scoreBean.setCurrent_money(15000);
			else if(scoreBean.getCurrent_money()==15000) scoreBean.setCurrent_money(25000);
			else if(scoreBean.getCurrent_money()==25000) scoreBean.setCurrent_money(50000);
			else if(scoreBean.getCurrent_money()==50000) scoreBean.setCurrent_money(100000);
			else if(scoreBean.getCurrent_money()==100000) scoreBean.setCurrent_money(150000);
			else if(scoreBean.getCurrent_money()==150000) scoreBean.setCurrent_money(250000);
			else if(scoreBean.getCurrent_money()==250000) scoreBean.setCurrent_money(500000);
			else if(scoreBean.getCurrent_money()==500000) scoreBean.setCurrent_money(1000000);
			else if(scoreBean.getCurrent_money()==1000000) scoreBean.setCurrent_money(2000000);
			else if(scoreBean.getCurrent_money()==2500000) scoreBean.setCurrent_money(5000000);
			else if(scoreBean.getCurrent_money()==5000000) redirectExit();
			forward();
		}
		else {
			if(scoreBean.getCurrent_money()<=10000) scoreBean.setCurrent_money(0);
			else if(scoreBean.getCurrent_money()<150000 && scoreBean.getCurrent_money()>=10000) scoreBean.setCheck_point(10000);
			else if(scoreBean.getCurrent_money()>=150000) scoreBean.setCheck_point(150000);
			redirectExit();
		}
	}
	public void check_D(){
		if(questionBean.getVariant_d().equals(questionBean.getAnswer())){
			questionViewed();
			if(scoreBean.getCurrent_money()==0) scoreBean.setCurrent_money(500);
			else if(scoreBean.getCurrent_money()==500) scoreBean.setCurrent_money(1000);
			else if(scoreBean.getCurrent_money()==1000) scoreBean.setCurrent_money(2000);
			else if(scoreBean.getCurrent_money()==2000) scoreBean.setCurrent_money(4000);
			else if(scoreBean.getCurrent_money()==5000) scoreBean.setCurrent_money(10000);
			else if(scoreBean.getCurrent_money()==10000) scoreBean.setCurrent_money(15000);
			else if(scoreBean.getCurrent_money()==15000) scoreBean.setCurrent_money(25000);
			else if(scoreBean.getCurrent_money()==25000) scoreBean.setCurrent_money(50000);
			else if(scoreBean.getCurrent_money()==50000) scoreBean.setCurrent_money(100000);
			else if(scoreBean.getCurrent_money()==100000) scoreBean.setCurrent_money(150000);
			else if(scoreBean.getCurrent_money()==150000) scoreBean.setCurrent_money(250000);
			else if(scoreBean.getCurrent_money()==250000) scoreBean.setCurrent_money(500000);
			else if(scoreBean.getCurrent_money()==500000) scoreBean.setCurrent_money(1000000);
			else if(scoreBean.getCurrent_money()==1000000) scoreBean.setCurrent_money(2000000);
			else if(scoreBean.getCurrent_money()==2500000) scoreBean.setCurrent_money(5000000);
			else if(scoreBean.getCurrent_money()==5000000) redirectExit();
			forward();
		}
		else {
			if(scoreBean.getCurrent_money()<=10000) scoreBean.setCurrent_money(0);
			else if(scoreBean.getCurrent_money()<150000 && scoreBean.getCurrent_money()>=10000) scoreBean.setCheck_point(10000);
			else if(scoreBean.getCurrent_money()>=150000) scoreBean.setCheck_point(150000);
			redirectExit();
		}
	}
	@SuppressWarnings({ "static-access", "unchecked" })
	public void redirectExit(){
		Session session=new HibernateUtil().getSessionFactory().openSession();
		Transaction tx=session.beginTransaction();
		List<Question> questionDB=(List<Question>) session.createQuery("from Question where viewed = 1").list();
		for(int i=0;i<questionDB.size();i++){
			questionDB.get(i).setViewed(0);
		}
		tx.commit();
		session.close();
		try {
			FacesContext ctx = FacesContext.getCurrentInstance();
			ExternalContext ectx = ctx.getExternalContext();
			HttpServletRequest request = (HttpServletRequest) ectx.getRequest();
			HttpServletResponse response = (HttpServletResponse) ectx
					.getResponse();
			response.sendRedirect(request.getContextPath()
					+ "/faces/exit.xhtml");
		} catch (IOException ex) {
		}
	}
	@SuppressWarnings("static-access")
	public void questionViewed(){
		Session session=new HibernateUtil().getSessionFactory().openSession();
		Transaction tx=session.beginTransaction();
		Question question=(Question) session.get(Question.class, questionBean.getId());
		question.setViewed(1);
		tx.commit();
		session.close();
	}
}
